<?php
// +----------------------------------------------------------------------
// | 麦沃德科技赋能开发者，助力中小企业发展
// +----------------------------------------------------------------------
// | Copyright (c) 2017～2024  www.wdadmin.cn    All rights reserved.
// +----------------------------------------------------------------------
// | Wdadmin系统产品软件并不是自由软件，不加密，并不代表开源，未经许可不可自由转售和商用
// +----------------------------------------------------------------------
// | Author: MY WORLD Team <bd@maiwd.cn>   www.wdadmin.cn
// +----------------------------------------------------------------------
/**
 * Class Withdrawal
 * Desc  提现Model
 * Create on 2024/11/5 16:59
 * Create by wangyafang
 */

namespace app\api\model\wddj\driver;


use think\Model;

class Withdrawal extends Model
{
// 表名
    protected $name = 'wddj_withdrawal';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    /**
     * Desc 获取提现数据
     * Create on 2024/11/5 17:23
     * Create by wangyafang
     */
    public static function getWithdrawalData($driverObj)
    {
        $where['driver_id'] = array('eq',$driverObj->id);
        $where['wechat_id'] = array('eq',$driverObj->wechat_id);
        $whereIncomeExpenditure['group_type'] = array('eq','1');
        $total_income = (new \app\api\model\wddj\driver\IncomeExpenditure())
            ->where($where)
            ->where($whereIncomeExpenditure)
            ->where('operate','1')
            ->sum('amount');

        $already_withdrawn = (new \app\api\model\wddj\driver\IncomeExpenditure())
            ->where($where)
            ->where($whereIncomeExpenditure)
            ->where('operate','2')
            ->sum('amount');//已提现

        $progress_withdrawn = (new \app\api\model\wddj\driver\Withdrawal())
            ->where($where)
            ->where('state','1')
            ->sum('amount');//申请体现中
        $intermediateResult = bcsub($total_income, $already_withdrawn, 2);

        $pending_withdrawal = bcsub($intermediateResult, $progress_withdrawn, 2);

        if ($pending_withdrawal == '0.00') {
            $pending_withdrawal = 0;
        }

        if ($progress_withdrawn == '0.00') {
            $progress_withdrawn = 0;
        }

        $data = array(
            'total_income'=>$total_income,
            'already_withdrawn'=>$already_withdrawn,//已提现
            'pending_withdrawal'=>$pending_withdrawal,//待提现
            'progress_withdrawn'=>$progress_withdrawn,//待提现
        );

        return $data;
    }
}



 